﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices;
    using System.Diagnostics;
    using System.Threading;
    using System.Security.Principal; 
    #endregion

    /// <summary>
    /// Objects that are Users or Groups
    /// </summary>
    [Serializable]
    public class UserGroup : UserGroupComputer, IUserGroup {

        #region Members
        public static readonly new string[] AttributesToGet;

        public IExchangeBaseAttributes ExchangeBaseAttributes { get; protected set; }
        public IExchangeExtensionAttributes ExchangeExtensionAttributes { get; protected set; }

        /// <summary>
        /// The SMTP email address
        /// </summary>
        public string Mail { get; protected set; }
        #endregion

        #region Constructors
        static UserGroup() {
            var attributesToGet = new List<string>();

            attributesToGet.AddRange(new string[]
            {
                "mail"
            });

            attributesToGet.AddRange(UserGroupComputer.AttributesToGet);
            attributesToGet.AddRange(Contoso.ActiveDirectory.ExchangeBaseAttributes.AttributesToGet);
            attributesToGet.AddRange(Contoso.ActiveDirectory.ExchangeExtensionAttributes.AttributesToGet);

            AttributesToGet = attributesToGet.Distinct().ToArray();
            attributesToGet = null;
        }
        public UserGroup() {
            this.Initialize();
        } 

        public UserGroup(DirectoryEntry directoryEntry, bool destroyInputObject = true)
            : base(directoryEntry, destroyInputObject) {
                if (this.GetType().Name == typeof(UserGroup).Name) {
                this.Attributes = null;
            }
        }

        public UserGroup(SearchResult result, bool destroyInputObject = true)
            : base(result, destroyInputObject) {
                if (this.GetType().Name == typeof(UserGroup).Name) {
                this.Attributes = null;
            }
        }

        public UserGroup(List<string> ldifOutput)
            : base(ldifOutput) {
                if (this.GetType().Name == typeof(UserGroup).Name) {
                this.Attributes = null;
            }
        }
        #endregion

        #region Methods
        protected override void Initialize() {
            base.Initialize();

            this.Mail = string.Empty;

            this.ExchangeBaseAttributes = new ExchangeBaseAttributes();
            this.ExchangeExtensionAttributes = new ExchangeExtensionAttributes();
        }

        protected override void ProcessResults() {

            try {
                base.ProcessResults();

                if (this.Attributes.ContainsKey("mail")) this.Mail = this.Attributes["mail"][0].ToString();
                this.ExchangeBaseAttributes.ProcessResults(this.Attributes);
                this.ExchangeExtensionAttributes.ProcessResults(this.Attributes);

                this.IsValid = !string.IsNullOrWhiteSpace(this.DN)
                    && this.ObjectClass.Any(x => 
                        string.Equals(x, "user", StringComparison.OrdinalIgnoreCase)
                        || string.Equals(x, "group", StringComparison.OrdinalIgnoreCase));
            }
            catch (Exception e) {
                Debug.WriteLine(string.Format("{0}  [ThreadID: {1}]  {2} Error: {3}", DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId, CustomExtensions.CurrentMethodName(), e.VerboseExceptionString()));
                throw;
            }
        } 
        #endregion
    }
}
